package com.dmcb.trade.business.crawlers.article;

import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

/**
 * 搜狐文章解析
 * Created by long on 2016/3/11.
 */
@Service
public class SohuParser extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.SOHU_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "h3.article-title,h2.title-info,h1,h1[itemprop = headline]";
    }

    /**
     * 获取日期选择器
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "footer.time,span#news-time,span#pubtime_baidu,div.time-source>div.time,div.news-writer>span.time,div#pubtime_baidu";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.display-content,article.article,div[itemprop=articleBody],div#contentText,div.pic-main";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.display-content img[src],article.article>p img[src],div#contentText>div>div img[src],div.text-pic>table>tbody>tr>td img[src],div#contentText>div>table>tbody>tr>td img[src],div#contentText>p img[src],div#contentText>div>p img[src],img#coverImg";
    }

    /**
     * 获取封面选择器 多个
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelectMulti() {
        return "div[itemprop=articleBody] img[style],div#contentText img[style]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "header.name,h4>a,span.writer>a";
    }

    @Override
    protected void setPostDate(Document doc, Article article) {
        Element element = doc.select(dateSelect()).first();
        if (element == null) {
            return;
        }
        String time  = element.text();
        Date date = null;
        if(StringUtils.isBlank(time)){
            element = doc.select("div.article-info>span[data-val]").first();
            if(element == null){
                return;
            }
            time = element.attr("data-val");
            try {
                if (StringUtils.isNotBlank(time)) {
                    time = time.substring(0,time.length()-3);
                }
                 date = DateUtil.stampToDate(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
             date = DateUtil.parseDate(time);
        }
        article.setPostTime(date);
    }

    @Override
    protected String platform() {
        return "搜狐网";
    }
}
